﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAControls
{
    public abstract class XNAControl
    {
        private Point position;

        /// <summary>
        /// Pozycja względem rodzica lub bezwzględna w przypadku jego braku.
        /// </summary>
        public Point Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        /// <summary>
        /// Bezwzględna pozycja.
        /// </summary>
        public Point AbsolutePosition
        {
            get
            {
                if (Parent == null)
                {
                    return position;
                }
                else
                {
                    return new Point(Parent.Position.X + position.X, Parent.Position.Y + position.Y);
                }
            }
        }

        public Point Size { get; set; }

        private List<XNAControl> children;

        public List<XNAControl> Children { get { return children; } }

        public XNAControl Parent { get; set; }

        public bool Visible { get; set; }

        public XNAControl()
        {
            children = new List<XNAControl>();
            Position = new Point(0, 0);
            Size = new Point(100, 100);
            Visible = true;
        }

        public void AddChild(XNAControl control)
        {
            control.Parent = this;
            children.Add(control);
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (Visible)
            {
                foreach (XNAControl xnaControl in children)
                {
                    xnaControl.Draw(spriteBatch);
                }
            }
        }

        public bool IsOverlapping(Point point)
        {
            return point.X >= Position.X && point.Y >= Position.Y && point.X <= Position.X + Size.X &&
                   position.Y <= Position.Y + Size.Y;
        }
    }
}
